package com.lagou.module01.servlet;

import com.lagou.module01.entity.PageBean;
import com.lagou.module01.entity.Student;
import com.lagou.module01.service.StudentService;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "FindUserByPageServlet", urlPatterns = "/findUserByPage")
public class FindUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求中的当前页码和每页显示条数的参数
        String page = request.getParameter("page");


        //这里创建默认值为1，也就是第一个默认查询第一页数据内容
        PageBean pageBean = new PageBean(1, 5);
        if (null != page && page.length() > 0) {
            pageBean.setPage(Integer.parseInt(page));
        }

        //调用StudentService的分页查询显示学生信息的方法
        StudentService studentService = new StudentService();
        List<Student> studentList = studentService.studentShow(pageBean);
        //调用StudentService的selectSumPage方法查询数据表总条数
        int sumPage = studentService.selectSumPage();

        //将List对象和数据表当前的总条数放入session对象中
        request.getSession().setAttribute("studentList", studentList);
        request.getSession().setAttribute("sumPage",sumPage);
        //重定向到StudentManage.jsp中
        response.sendRedirect("StudentManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
